package cj.web.activity.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.activity.constants.SCGoodsStatusEnum;
import cj.web.activity.constants.SCGoodsTypeEnum;
import cj.web.activity.dao.SCGoodsDAO;
import cj.web.activity.domain.SCGoods;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.domain.UserApplication;

@Path("/scGoods")
public class SCGoodsController extends BaseController{

	private static Logger logger = Logger.getLogger(SCGoodsController.class.getName());
	
	@Autowired
	private SCGoodsDAO scGoodsDAO;
	
	@Get("show")
	public String show(Model model,UserApplication userApplication) {
		model.add("TypeEnum",EnumSet.allOf(SCGoodsTypeEnum.class));
		
		model.add("StatusEnum",EnumSet.allOf(SCGoodsStatusEnum.class));
		//MemuEnum
        model.add("MemuEnum",MemuEnum.scgoods);
		return "/views/activity/sc/scGoodsList.vm";
	}
	
	@Post("getList")
	public String queryList(SCGoods scGoods) {
		
		JSONObject jo = new JSONObject();
		List<SCGoods> objs = scGoodsDAO.getList(scGoods);
		
		int size = scGoodsDAO.getSize(scGoods);
		jo.put("data", objs);
		jo.put("draw", scGoods.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.scgoods);

        model.add("TypeEnum",EnumSet.allOf(SCGoodsTypeEnum.class));
		model.add("StatusEnum",EnumSet.allOf(SCGoodsStatusEnum.class));
		
		return "/views/activity/sc/scGoodsDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,SCGoods scGoods,@Param("umContent") String umContent,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		scGoods.setCreateUser(returnSessionObj(request).getAccount());
		scGoods.setUpdateUser(returnSessionObj(request).getAccount());
		scGoods.setDescs(umContent);
		
		try {
			scGoodsDAO.create(scGoods);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") String id)
	{
		model.add("MemuEnum",MemuEnum.scgoods);

        model.add("TypeEnum",EnumSet.allOf(SCGoodsTypeEnum.class));
		model.add("StatusEnum",EnumSet.allOf(SCGoodsStatusEnum.class));
		
		SCGoods obj = scGoodsDAO.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
			if(obj.getImage() !=null && obj.getImage().length() > 0){
				String[] arr = obj.getImage().split("/");
				if(arr.length == 2){
					model.add("showImg", arr[0] + "/s_" + arr[1]);
				}else{
					model.add("showImg", "");
				}
			}else{
				model.add("showImg", "");
			}
		}
		return "/views/activity/sc/scGoodsEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SCGoods scGoods,@Param("umContent") String umContent,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		scGoods.setUpdateUser(returnSessionObj(request).getAccount());
		scGoods.setDescs(umContent);
		
		try {
			scGoodsDAO.update(scGoods);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("up")
	public String up(@Param("id") String id) {
		SCGoods obj = scGoodsDAO.queryById(id);

		if (obj != null) {
			scGoodsDAO.changeStatus(obj.getId().toString(),SCGoodsStatusEnum.SPSJ.getValue());
		}

		return "r:show";
	}
	
	@Get("down")
	public String down(@Param("id") String id) {
		SCGoods obj = scGoodsDAO.queryById(id);

		if (obj != null) {
			scGoodsDAO.changeStatus(obj.getId().toString(),SCGoodsStatusEnum.SPXJ.getValue());
		}

		return "r:show";
	}
	
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/s_"+fileName, 80, 80, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
}
